import java.util.Scanner

class Board {
    //定义一个棋盘二维数组，长度为15，宽为15
    var board = Array(15) { CharArray(15) }
    //初始化棋盘
    init {
        originBoard()
    }
    //生成棋盘，用二维数组如果是0，0为空；0，0-14为A-N，0-14，0为A-N，其余全为+
    fun originBoard() {
        for (i in 0 until 15) {
            for (j in 0 until 15) {
                if (i == 0 && j == 0) {
                    board[0][0] = ' '
                } else if (i == 0) {
                    board[i][j] = ('A' + j - 1).toChar() //强制转换
                } else if (j == 0) {
                    board[i][j] = ('A' + i - 1).toChar()
                } else {
                    board[i][j] = '+'

                }

            }
        }
    }
    //打印棋盘
    fun printBoard() {
        for (i in 0 until 15) {
            for (j in 0 until 15) {
                print("${board[i][j]} ")
            }
            //确保换行
            println()

        }
    }
    //输出Boolean类型，来确保胜利的ture可以被下面判断胜利的if条件接受并执行，否则游戏继续
    fun checkWin(chess: Char): Boolean {
        // 检查水平方向，第一次为判断这个棋子是不是chess，val chess = if (i % 2 == 1) '●' else '○'
        //第二次判断为在第一次条件下用二维数组的相等表示剩下的4个棋子相等
        for (i in 1..10) {
            for (j in 1 until 15) {
                if (board[i][j] == chess &&
                    board[i][j] == board[i + 1][j] &&
                    board[i][j] == board[i + 2][j] &&
                    board[i][j] == board[i + 3][j] &&
                    board[i][j] == board[i + 4][j]) {
                    return true
                }
            }
        }

        // 检查垂直方向
        for (i in 1 until 15) {
            for (j in 1..10) {
                if (board[i][j] == chess &&
                    board[i][j] == board[i][j + 1] &&
                    board[i][j] == board[i][j + 2] &&
                    board[i][j] == board[i][j + 3] &&
                    board[i][j] == board[i][j + 4]) {
                    return true
                }
            }
        }

        // 检查主对角线方向
        for (i in 1..10) {
            for (j in 1..10) {
                if (board[i][j] == chess &&
                    board[i][j] == board[i + 1][j + 1] &&
                    board[i][j] == board[i + 2][j + 2] &&
                    board[i][j] == board[i + 3][j + 3] &&
                    board[i][j] == board[i + 4][j + 4]) {
                    return true
                }
            }
        }

        // 检查次对角线方向
        for (i in 1..10) {
            for (j in 5 until 15) {
                if (board[i][j] == chess &&
                    board[i][j] == board[i + 1][j - 1] &&
                    board[i][j] == board[i + 2][j - 2] &&
                    board[i][j] == board[i + 3][j - 3] &&
                    board[i][j] == board[i + 4][j - 4]) {
                    return true
                }
            }
        }
        //确保输出false不触发胜利判断
        return false
    }
}

class Person(val board: Board) {
    fun playChess() {
        var i = 1
        while (true) {
            println("请输入落子坐标：")
            val scanner = Scanner(System.`in`)
            val x = scanner.nextInt()
            val y = scanner.nextInt()
            //确保输入棋盘的数字时不会越界
            if (x < 0 || x >= 15 || y < 0 || y >= 15) {
                println("坐标越界，请重新输入。\n")
                continue
            }
            //循环切换黑棋和白棋，同时确保不会重复下棋，continue为跳回原来的循环，即继续循环。break为跳出循环
            val chess = if (i % 2 == 1) '●' else '○'
            if (board.board[x][y] == '+') {
                board.board[x][y] = chess
            } else {
                println("该位置已经有棋子了，请重新输入。\n")
                continue
            }
            board.printBoard()
            //检查胜利条件
            if (board.checkWin(chess)) {
                println("$chess 获胜！")
                break
            }

            i++

        }
    }
}



fun main() {
    val board = Board()
    board.printBoard()
    val person = Person(board)
    person.playChess()
}